package com.reflect;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * @ClassName ReflectDemo2
 * @Description 反射，突破泛型约束
 * @Author 2214361854
 * @Date 2022/3/4 21:12
 * @Version 1.0
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws Exception {
        //通过反射，给list添加元素（不被泛型约束）
        ArrayList<String> list1 = new ArrayList();
        ArrayList<Integer> list2 = new ArrayList();

        System.out.println(list1.getClass());
        System.out.println(list2.getClass());
        System.out.println(list1.getClass()==list2.getClass());

        System.out.println("=====================");
        list2.add(12);
        list2.add(13);

        //突破泛型约束
        Class c2 = list2.getClass();
        Method add = c2.getDeclaredMethod("add",Object.class);
        add.invoke(list2,"小王");
        System.out.println(list2);

        //突破泛型约束
        ArrayList list3= list2;
        list3.add("你好");
        System.out.println(list2);

    }
}
